/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controllers.sysAdmin;

import controllers.utils.DatabaseHjelper;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;
import models.Department;

/**
 *
 * @author Ida
 */
@SessionScoped
@Named(value="newDepartment")
public class addNewDepartment implements Serializable {
    private Department newDepartment;
    private DatabaseHjelper dbh = DatabaseHjelper.hent();

    public addNewDepartment() {
        this.newDepartment = new Department();
    }

    public Department getNewDepartment() {
        return newDepartment;
    }

    public String addNewDepartment(){
        newDepartment.setName(null);
        newDepartment.setAddress(null);
        newDepartment.setPhoneNr(null);
        newDepartment.setNote(null);
        return "addNewDepartment";
    }

     public String executeNewDepartment(){
        addNewDepartmentToDB();
        return "addNewDepartmentOutput";
    }

      private void addNewDepartmentToDB() {
        Connection conn = null;
        PreparedStatement stmt = null;
        try {
            conn = dbh.finnForbindelse();
            stmt = conn.prepareStatement("INSERT INTO department(departmentName, depAddress, depPhoneNr, depNote, instituteID) VALUES(?, ?, ?, ?)");
            stmt.setString(1, newDepartment.getName());
            stmt.setString(1, newDepartment.getAddress());
            stmt.setString(2, newDepartment.getPhoneNr());
            stmt.setString(3, newDepartment.getNote());
            stmt.setInt(4, newDepartment.getInstitute().getInstituteID());
            stmt.executeUpdate();
        } catch (SQLException e) {
            System.out.println("Feil i lagring av bruker: " + e);
        } finally {
            DatabaseHjelper.lukk(stmt);
            DatabaseHjelper.lukk(conn);
        }
    }

}
